package fr.insee.rome.string.norm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import fr.insee.rome.factory.RomeFactory;


public class SimpleNormalizationSteps implements RomeNormalizationSteps {

	private static RomeNormalizationSteps instance = null;
	private final String
		blancsMultiples = "\\s+",
		caracteresSpeciaux = "\\W+"
	;
	private Synonymes synonymes;
	
	private SimpleNormalizationSteps(){
		synonymes = RomeFactory.getSynonymes();
	}
	
	public static RomeNormalizationSteps getInstance(){
		if(instance == null){
			instance = new SimpleNormalizationSteps();
		}
		return instance;
	}

	public String mettreEnMajuscule(String x) {
		return x.toUpperCase();
	}

	public String supprimerCaracteresSpeciaux(String x) {
		return x.replaceAll(caracteresSpeciaux, " ");
	}

	public String remplacerLesSynonymes(String x) {
		
		for(Pattern pattern : synonymes){
			Matcher matcher = pattern.matcher(x);
			if(matcher.find()){
				x = matcher.replaceAll(" " + synonymes.get(pattern) + " ");
			}
			matcher = null;
		}
		return x;
	}

	public List<String> separerMots(String x) {
		return Arrays.asList(x.split(blancsMultiples));
	}

	public List<String> supprimerMotsCourts(List<String> mots) {
		
		List<String> result = new ArrayList<String>(mots.size());
		for(String mot : mots){
			if(mot.length() > 3){
				result.add(mot);
			}
		}
		return result;
	}

	public Set<String> supprimerMotsEnDouble(List<String> mots) {
		return new LinkedHashSet<String>(mots);
	}
}
